package com.funny.stock.trade.service.monitor;

import com.alibaba.fastjson.JSON;
import com.funny.stock.base.redis.RedisTool;
import com.funny.stock.base.util.PropertiesCopyUtil;
import com.funny.stock.data.collect.StockMonitorStrategy;
import com.funny.stock.data.collect.bean.StockMonitorContext;
import com.funny.stock.data.vo.RealtimeStockData;
import com.funny.stock.data.vo.SimpleRealtimeStockData;
import com.funny.stock.data.vo.StockConstants;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 实时更新股票池的数据信息
 *
 * @author pengl
 * @date 2020/11/11
 */
@Component
public class StockDataMonitorService implements StockMonitorStrategy {
    private static final Logger logger = LoggerFactory.getLogger(StockDataMonitorService.class);

    @Autowired
    RedisTool redisTool;

    @Override
    public void onMonitor(StockMonitorContext context) {
        Map<String, RealtimeStockData> dataMap = context.getDataMap();

        for (String key : dataMap.keySet()) {
            RealtimeStockData realtimeStockData = dataMap.get(key);
            if(realtimeStockData == null) continue;

            String stockKey = StockConstants.KEY_STOCK_REALTIME_DATA_PREFIX + realtimeStockData.getSymbol() ;
            String simpleStockKey = StockConstants.KEY_STOCK_REALTIME_SIMPLE_DATA_PREFIX ;
            redisTool.set(stockKey,JSON.toJSONString(realtimeStockData));
            redisTool.expire(stockKey, StockConstants.KEY_DEFAULT_EXPIRE_HOURS, TimeUnit.HOURS);

            SimpleRealtimeStockData simpleRealtimeStockData = PropertiesCopyUtil.copyProperties(realtimeStockData, SimpleRealtimeStockData.class);
            redisTool.hPut(simpleStockKey,simpleRealtimeStockData.getSymbol(),simpleRealtimeStockData);
            redisTool.expire(simpleStockKey, StockConstants.KEY_DEFAULT_EXPIRE_HOURS, TimeUnit.HOURS);
        }
    }



}
